<template>
    <div class="login_container">
        <!-- 登录块 -->
        <div class="login_box">
            <!-- 头像块 -->
            <div class="avatar_box">
                <img src="../assets/logo.png">
            </div>

            <!-- 表单区域 -->
            <el-form ref="loginFormRef" :rules="loginRules" :model="loginForm" class="login_form" label-width="0">
                <!-- 用户名 -->
                <el-form-item prop="username">
                    <el-input v-model="loginForm.username" prefix-icon="iconfont icon-guanliyuan">
                    </el-input>
                </el-form-item>
                <!-- 密码 -->
                <el-form-item prop="password">
                    <el-input type="password" v-model="loginForm.password" prefix-icon="iconfont icon-tianchongxing-">
                    </el-input>
                </el-form-item>
                <!-- 按钮 -->
                <el-form-item class="btns">
                    <el-button type="primary" @click="login()">提交</el-button>
                    <el-button type="info" @click="resetLoginForm('loginFormRef')">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
export default {
    data(){ 
        return {
            loginForm: {
                username: "admin",
                password: "123456"
            },
            loginRules: {
                username: [
                    { required: true, message: '用户名不能为空', trigger: 'blur' },//必填项
                    { min: 3, max: 7, message: '用户名在 3 到 7 个字符', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '密码不能为空', trigger: 'blur' },//必填项
                    { min: 6, max:12, message: '密码长度在 6 到 12 个字符', trigger: 'blur' }
                ],
            },
        }
    },
    methods: {
        //重置表单
        resetLoginForm(loginFormRef) { 
            this.$refs.loginFormRef.resetFields();
        },
        login() { 
            this.$refs.loginFormRef.validate(valid => { 
                if (!valid) return;//如果不通过校验则退出login函数
                this.axios.post("/login",this.loginForm).then(res => { //访问后台
                    if (res.data.flag == 'success') {
                        this.$message.success('操作成功')

                        //用户信息存储到session中
                        window.sessionStorage.setItem("user", res.data.user)
                        // console.log(res.data.user)
                        
                        this.$router.push({ path: '/home' })//路由跳转

                    } else {
                        this.$message.error('操作失败')
                    }
                })
           
            })
        }
    }
}
</script>

<style lang="less" scoped>

.login_container {
    height: 100%;
    width: 100%;
    background: #2b4b6b;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    .login_box{
        width: 450px;
        height: 350px;
        background: #fff;
        border-radius: 3px;
        display: flex;
        flex-direction: column;
        align-items: center;
  
        .avatar_box{
            width: 130px;
            height: 130px;
            border: 1px solid #eee;
            border-radius: 50%;
            img{
                width: 100%;
                height: 100%;
                border-radius: 50%;
                background: #eee;
            }
        }
      
    }
          .btns {
              display: flex;
              justify-content: flex-end;
              margin: 0 100px;
          }
          .login_form{
            margin: 10px 0;
            box-sizing: border-box;
          }
}
    
</style>